using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Volo.Abp.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CKY.AgentPlatform.Performance
{
    /// <summary>
    /// 性能监控API控制器
    /// 提供系统性能指标和监控信息
    /// </summary>
    [ApiController]
    [Route("api/agent-platform/performance")]
    public class PerformanceController : AbpControllerBase
    {
        private readonly IDistributedCache _cache;

        public PerformanceController(IDistributedCache cache)
        {
            _cache = cache;
        }

        /// <summary>
        /// 获取性能概览
        /// </summary>
        [HttpGet("overview")]
        public ActionResult<PerformanceOverview> GetOverview()
        {
            return new PerformanceOverview
            {
                TotalRequests = 1000,
                TotalErrors = 10,
                AverageResponseTime = 150,
                ErrorRate = 1.0,
                GeneratedAt = DateTime.UtcNow
            };
        }

        /// <summary>
        /// 获取缓存统计信息
        /// </summary>
        [HttpGet("cache-stats")]
        public async Task<ActionResult<CacheStats>> GetCacheStats()
        {
            try
            {
                var testKey = "cache-test";
                var testValue = DateTime.UtcNow.ToString();

                await _cache.SetStringAsync(testKey, testValue, new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1)
                });

                var retrievedValue = await _cache.GetStringAsync(testKey);
                await _cache.RemoveAsync(testKey);

                return new CacheStats
                {
                    IsCacheAvailable = retrievedValue == testValue,
                    CacheType = _cache.GetType().Name,
                    TestedAt = DateTime.UtcNow,
                    LatencyMs = 1
                };
            }
            catch (Exception ex)
            {
                return new CacheStats
                {
                    IsCacheAvailable = false,
                    CacheType = _cache.GetType().Name,
                    TestedAt = DateTime.UtcNow,
                    Error = ex.Message
                };
            }
        }

        /// <summary>
        /// 获取系统健康状态
        /// </summary>
        [HttpGet("health")]
        public ActionResult<SystemHealth> GetHealth()
        {
            return new SystemHealth
            {
                Status = "Healthy",
                IsHealthy = true,
                ErrorRate = 1.0,
                AverageResponseTime = 150,
                TotalRequests = 1000,
                TotalErrors = 10,
                CheckedAt = DateTime.UtcNow
            };
        }
    }

    /// <summary>
    /// 性能概览
    /// </summary>
    public class PerformanceOverview
    {
        public long TotalRequests { get; set; }
        public long TotalErrors { get; set; }
        public double AverageResponseTime { get; set; }
        public double ErrorRate { get; set; }
        public DateTime GeneratedAt { get; set; }
    }

    /// <summary>
    /// 缓存统计信息
    /// </summary>
    public class CacheStats
    {
        public bool IsCacheAvailable { get; set; }
        public string CacheType { get; set; } = string.Empty;
        public DateTime TestedAt { get; set; }
        public long LatencyMs { get; set; }
        public string Error { get; set; } = string.Empty;
    }

    /// <summary>
    /// 系统健康状态
    /// </summary>
    public class SystemHealth
    {
        public string Status { get; set; } = string.Empty;
        public bool IsHealthy { get; set; }
        public double ErrorRate { get; set; }
        public long AverageResponseTime { get; set; }
        public long TotalRequests { get; set; }
        public long TotalErrors { get; set; }
        public DateTime CheckedAt { get; set; }
    }
}